import org.junit.Test;

import java.util.Arrays;

/**
 * 给定一个数组含零数组,将0向后移动,原有的数组的顺序不变
 */
public class mobileZero {

    @Test
    public void clibe() {
        int[] nums = {0, 9, 0, 2, 3, 0};

        long startTime1 =  System.currentTimeMillis();
        int[] ints = mobileZero(nums);
        long endTime1 =  System.currentTimeMillis();
        System.out.println("sum1=" + Arrays.toString(ints)+",使用时间:"+ (endTime1-startTime1));


    }

    /**
     * 使用双指针,
     */
    public int[] mobileZero(int[] nums) {
        if (nums == null) {
            return null;
        }
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[j++] = nums[i];
            }
        }

        for (int i = j; i < nums.length; i++) {
            nums[i] = 0;
        }
        return nums;
    }
}
